from operator import le
from PyQt5 import uic
from PyQt5.QtWidgets import QApplication,QTableWidgetItem
from bulidExcel import buildData
import json
class MainWindows:
    def __init__(self):
        # 从文件中加载UI定义
        self.ui = uic.loadUi("TransformExcel.ui")
        self.datas = {}
        self.ui.previewBtn.clicked.connect(self.preViewData)
        buildJson = '{"headNames":["aa","bb"],"load":false,"importData":false,"predefinedValue":[["aa","bb"],[],[],[],[]],"file":"c:\\\\data.xlsx","calcRules":[{"predefinedValue":[{"headName":"问题类型","valueRanges":["故事","任务"]}],"amount":1000}]}'
        self.ui.buildText.setPlainText(json.dumps(json.loads(buildJson), indent=2,ensure_ascii=False))

    def preViewData(self):
        buildText = self.ui.buildText
        self.datas = buildData(buildText.toPlainText())
        if len(self.datas) > 0:
            table = self.ui.dataTable
            table.verticalHeader().setVisible(False)
            table.setRowCount(len(self.datas)-1)  
            headNames = self.datas[0]
            table.setColumnCount(len(headNames))   
            ### 设置表头
            for col in range(len(headNames)):
                table.setHorizontalHeaderItem(col,QTableWidgetItem(headNames[col]))
            ### 设置数据
            for row in range(len(self.datas)):
                if row == 0:
                    continue
                data = self.datas[row]
                for col in range(len(data)):
                    item = QTableWidgetItem()
                    item.setText(data[col+1])
                    table.setItem(row,col,item)
    
app = QApplication([])
stats = MainWindows()
stats.ui.show()
app.exec_()        